package com.cyz.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.common.bean.RoleMenu;
import com.cyz.common.mapper.RoleMenuMapper;
import com.cyz.common.service.RoleMenuService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author 34928
* @description 针对表【role_menu(角色和菜单关联表)】的数据库操作Service实现
* @createDate 2023-02-24 09:32:24
*/
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu>
    implements RoleMenuService {

    @Override
    public List<Integer> getMenuIdsByRoleIds(List<Integer> roleIds) {
        //创建不重复数组
        Set<Integer> set = new HashSet<>();
        //添加数据
        roleIds.forEach(item -> {
                    set.addAll(getMenuIdsByRoleId(item));
                });
        return new ArrayList<>(set);
    }

    public List<Integer> getMenuIdsByRoleId(Integer roleId) {
        //创建筛选器
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据角色id查询
        lambdaQueryWrapper.eq(RoleMenu::getRoleId,roleId);
        //查询数据
        List<RoleMenu> list = list(lambdaQueryWrapper);
        //转化数据
        List<Integer> collect = list.stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());
        return collect;
    }
}




